Game đuổi chó sa mạc lập trình bằng ngôn ngữ c#
19.239 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class Player : MonoBehaviour {
5 public GameManager gameManager;
6 public float pos = 0f;
7 float speed = 0f;
8 int direction = 0;
9
10 public AudioClip hitClip, jumpClip, deathClip, goodClip;
11 AudioSource hitSound, jumpSound, deathSound, goodSound;
12
13 void Start () {
14 hitSound = gameObject.AddComponent<AudioSource>();
15 jumpSound = gameObject.AddComponent<AudioSource>();
16 deathSound = gameObject.AddComponent<AudioSource>();
17 goodSound = gameObject.AddComponent<AudioSource>();
18 hitSound.clip = hitClip;
19 jumpSound.clip = jumpClip;
20 deathSound.clip = deathClip;
21 goodSound.clip = goodClip;
22
23 pos = 0f;
24 speed = 0f;
25 GetComponent<Rigidbody2D>().isKinematic = true;
26 }
27
28 public void StartPlayer()
29 {
30 GetComponent<Rigidbody2D>().isKinematic = false;
31 OnJump();
32 }
33
34 void AddSpeed(float delta)
35 {
36 speed = Mathf.Clamp( speed + delta, -1f, 1f);
37 }
38
39 void SetSpeed(float spd)
40 {
41 speed = spd;
42 }
43 public void OnAttack()
44 {
45 }
46 public void OnJump()
47 {
48 //if (!gameManager.isGameStart && !gameManager.isGameOver) gameManager.StartGame();
49 jumpSound.Play();
50 GetComponent<Rigidbody2D>().velocity = Vector2.zero;
51 if (transform.position.y > 8f) return;
52 GetComponent<Rigidbody2D>().AddForce(Vector2.up * 500);
53 }
54 public void OnRight()
55 {
56 direction = 1;
57 }
58 public void OnLeft()
59 {
60 direction = -1;
61 }
62 public void OnStop()
63 {
64 direction = 0;
65 SetSpeed(0f);
66 }
67
68 void UpdateMovement()
69 {
70 if (direction != 0) AddSpeed(direction * 0.01f);
71 pos += speed * Time.deltaTime;
72 }
73
74 public void PlayGoodSound()
75 {
76 goodSound.Play();
77 }
78
79 void DamageEffect()
80 {
81 hitSound.Play();
82 deathSound.Play();
83 gameManager.ShakeCam();
84 gameManager.DamageEffect();
85 }
86
87 void OnTriggerEnter2D(Collider2D other)
88 {
89 if (other.name == "pillar")
90 {
91 if (!gameManager.isGameOver) DamageEffect();
92 //other.collider2D.enabled = false;
93 gameManager.StopGame();
94 }
95 else
96 {
97 OnStop();
98 if (!gameManager.isGameOver) DamageEffect();
99 gameManager.StopGame();
100 GetComponent<Rigidbody2D>().isKinematic = true;
101 //hitSound.Play();
102 }
103 }
104
105 void Update () {
106 UpdateMovement();
107 if (transform.position.y < -2f) transform.eulerAngles = new Vector3(0f, 0f, -60f);
108 else transform.eulerAngles = new Vector3(0f, 0f, GetComponent<Rigidbody2D>().velocity.y * 2f);
109 }
110 }
2 using System.Collections;
3
4 public class Player : MonoBehaviour {
5 public GameManager gameManager;
6 public float pos = 0f;
7 float speed = 0f;
8 int direction = 0;
9
10 public AudioClip hitClip, jumpClip, deathClip, goodClip;
11 AudioSource hitSound, jumpSound, deathSound, goodSound;
12
13 void Start () {
14 hitSound = gameObject.AddComponent<AudioSource>();
15 jumpSound = gameObject.AddComponent<AudioSource>();
16 deathSound = gameObject.AddComponent<AudioSource>();
17 goodSound = gameObject.AddComponent<AudioSource>();
18 hitSound.clip = hitClip;
19 jumpSound.clip = jumpClip;
20 deathSound.clip = deathClip;
21 goodSound.clip = goodClip;
22
23 pos = 0f;
24 speed = 0f;
25 GetComponent<Rigidbody2D>().isKinematic = true;
26 }
27
28 public void StartPlayer()
29 {
30 GetComponent<Rigidbody2D>().isKinematic = false;
31 OnJump();
32 }
33
34 void AddSpeed(float delta)
35 {
36 speed = Mathf.Clamp( speed + delta, -1f, 1f);
37 }
38
39 void SetSpeed(float spd)
40 {
41 speed = spd;
42 }
43 public void OnAttack()
44 {
45 }
46 public void OnJump()
47 {
48 //if (!gameManager.isGameStart && !gameManager.isGameOver) gameManager.StartGame();
49 jumpSound.Play();
50 GetComponent<Rigidbody2D>().velocity = Vector2.zero;
51 if (transform.position.y > 8f) return;
52 GetComponent<Rigidbody2D>().AddForce(Vector2.up * 500);
53 }
54 public void OnRight()
55 {
56 direction = 1;
57 }
58 public void OnLeft()
59 {
60 direction = -1;
61 }
62 public void OnStop()
63 {
64 direction = 0;
65 SetSpeed(0f);
66 }
67
68 void UpdateMovement()
69 {
70 if (direction != 0) AddSpeed(direction * 0.01f);
71 pos += speed * Time.deltaTime;
72 }
73
74 public void PlayGoodSound()
75 {
76 goodSound.Play();
77 }
78
79 void DamageEffect()
80 {
81 hitSound.Play();
82 deathSound.Play();
83 gameManager.ShakeCam();
84 gameManager.DamageEffect();
85 }
86
87 void OnTriggerEnter2D(Collider2D other)
88 {
89 if (other.name == "pillar")
90 {
91 if (!gameManager.isGameOver) DamageEffect();
92 //other.collider2D.enabled = false;
93 gameManager.StopGame();
94 }
95 else
96 {
97 OnStop();
98 if (!gameManager.isGameOver) DamageEffect();
99 gameManager.StopGame();
100 GetComponent<Rigidbody2D>().isKinematic = true;
101 //hitSound.Play();
102 }
103 }
104
105 void Update () {
106 UpdateMovement();
107 if (transform.position.y < -2f) transform.eulerAngles = new Vector3(0f, 0f, -60f);
108 else transform.eulerAngles = new Vector3(0f, 0f, GetComponent<Rigidbody2D>().velocity.y * 2f);
109 }
110 }
if (!gameManager.isGameStart && !gameManager.isGameOver) gameManager.StartGame();
other.collider2D.enabled = false;
hitSound.Play();